package com.ht.medicaltreatment.activity

import android.content.Intent
import android.view.View
import androidx.activity.viewModels
import com.blankj.utilcode.util.TimeUtils
import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.view.ProjectDialog
import com.ht.medicaltreatment.view.UserNameDialog
import com.ht.medicaltreatment.viewmodel.BorrowBuildViewModel
import com.ht.medicaltreatment.viewmodel.UserViewModel
import com.loper7.date_time_picker.DateTimeConfig
import com.loper7.date_time_picker.dialog.CardDatePickerDialog
import com.xgst.lib.utils.showToast
import kotlinx.android.synthetic.main.activity_build_borrow_order_layout.*

//创建借出表单
class BuildBorrowOrderActivity:BaseActivity(), View.OnClickListener {
    override fun getLayoutId() = R.layout.activity_build_borrow_order_layout

    private var projectId = 0
    private val model by viewModels<BorrowBuildViewModel>()
    private val userModel by viewModels<UserViewModel>()
    private var retrunTime = 0L

    override fun init() {
        setTitle("创建计划表单")
        tvProjectName.setOnClickListener(this)
        tvName.setOnClickListener(this)
        btnStart.setOnClickListener(this)
        tvReturnTime.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when(v){
            //项目
            tvProjectName -> {
                ProjectDialog(this,projectId).show {id,name ->
                    tvProjectName.text = name
                    projectId = id
                }
            }

            //取走人
            tvName -> {
                UserNameDialog(this).show{name,psw ->
                    showProgress()
                    userModel.getUserName(name,psw, {
                        dismissProgress()
                        tvName.text = it
                    },{
                        dismissProgress()
                        showToast(it)
                    })
                }
            }

            //选择时间
            tvReturnTime -> {
                CardDatePickerDialog.builder(this)
                    .setTitle("选择归还时间")
                    .showBackNow(false)
                    .showFocusDateInfo(false)
                    .setMinTime(System.currentTimeMillis())
                    .setThemeColor(getColor(R.color.blue))
                    .setWrapSelectorWheel(false)//循环滚动
                    .setBackGroundModel(CardDatePickerDialog.CUBE)
                    .setDisplayType(arrayListOf(
                        DateTimeConfig.YEAR,
                        DateTimeConfig.MONTH,
                        DateTimeConfig.DAY,
                        DateTimeConfig.HOUR, DateTimeConfig.MIN,DateTimeConfig.SECOND))
                    .setOnChoose {millisecond->
                        retrunTime = millisecond
                        tvReturnTime.text = TimeUtils.millis2String(millisecond,"yyyy-MM-dd HH:mm:ss")
                    }.build().show()
            }

            //创建
            btnStart -> {

                if(projectId == 0){
                    showToast("请选择项目")
                    return
                }

                if(tvName.text.toString().isEmpty()){
                    showToast("请选择取走人")
                    return
                }

                if(tvReturnTime.text.toString().isEmpty()){
                    showToast("请选择归还时间")
                    return
                }

                if(retrunTime - TimeUtils.getNowMills()  <= 0){
                    showToast("归还时间不能小于当前时间")
                    return
                }

                if(etName.text.toString().isEmpty()){
                    showToast("请输入表单名称")
                    return
                }
                val remark = etRemark.text.toString()
                showProgress()
                model.create(
                    tvReturnTime.text.toString(),
                    remark,
                    etName.text.toString(),
                    "$projectId",
                    tvName.text.toString()){
                    val intent = Intent(this,BorrowAddActivity::class.java)
                    intent.putExtra("projectName",tvProjectName.text.toString())
                    intent.putExtra("projectId","$projectId")
                    intent.putExtra("userName",tvName.text.toString())
                    intent.putExtra("returnTime",tvReturnTime.text.toString())
                    intent.putExtra("orderName",etName.text.toString())
                    intent.putExtra("id",it.id)

                    startActivity(intent)
                    finish()
                }
            }
        }
    }
}

